package org.example.service.impl;

import org.example.entity.Rabbit;
import org.example.entity.Student;
import org.example.entity.Teacher;
import org.example.entity.Team;
import org.example.service.RabbitPanel;
import org.example.service.RabbitService;
import org.example.util.RandomHelper;

import java.util.List;

public class RabbitServiceImpl implements RabbitService {

    private final Team team;
    private final Rabbit rabbit;

    static int BUYCARROTS_COSTCOIN=1;
    public RabbitServiceImpl(Team team, Rabbit rabbit) {
        this.team = team;
        this.rabbit = rabbit;
    }

    public Team getTeam() {
        return team;
    }

    public Rabbit getRabbit() {
        return rabbit;
    }

    @Override
    public void feed() {
        List<Student> students = team.getStudentLists();
        Teacher teacher = team.getTeacher();
        mainProcess(students,teacher,rabbit);
    }


    static void mainProcess(List<Student> students, Teacher teacher , Rabbit rabbit){

        while (rabbit.getWeight() < Rabbit.MAX_WEIGHT){
            Integer rangeClosed = RandomHelper.rangeClosed(2, 0);
            Student student = students.get(rangeClosed);
            // increase
            student.set_coins(student.get_coins() - BUYCARROTS_COSTCOIN);
            student.set_feedCount(student.get_feedCount() + 1);
            //
            rabbit.setWeight(rabbit.getWeight() + Rabbit.GROWTH_WEIGHT);
            //
            if (rabbit.getWeight() - rabbit.getLastWeightBeforeReward() >= Rabbit.WEIGHT_THRESHOLDVALUE){
                //
                if (teacher.get_coins() > Teacher.MIN_COINS){
                    student.set_coins(student.get_coins()+Teacher.REWARD_COINS);
                    teacher.set_coins(teacher.get_coins()-Teacher.REWARD_COINS);
                    student.set_rewardCount(student.get_rewardCount()+1);
                }
                // 满足if 存储
                rabbit.setLastWeightBeforeReward(rabbit.getWeight());
            }
        }

    }


}
